set more off
clear
cls

/*********************************************************************
Set the following local value to the directory containing the .do file
*********************************************************************/


global scripts_dir "C:\Users\Járay István\MTA_PTI\rr_legislative_stability\Repository\Scripts"
global figs_dir "..\FIGURES\"
global reg_est_dir "..\REGRESSION_TABLES\"
global other_tab_dir "..\OTHER_TABLES\"
cd "$scripts_dir"



///////// DESCRIPTIVE STATISTICS OF MODIFICATION NETWORK /////////

*importing modification network table containing descriptive statistics
*Váltltozólista (kulcs változók! Input és Output) + azok szűrése
import delimited "..\DATA\network_db_final.txt", clear
label variable passed_difference "Number of days between passage and modification (calendar days)"

// to generate figure and table with modifications in the same or next cycle
encode target_cycle, gen(target_cycle_fac) 
lab l target_cycle_fac
encode source_cycle, gen(source_cycle_fac)
lab l source_cycle_fac

/*For easier understanding the dummies below indicate whether the 
amendment was intra-term (same_cycle) or over-term (previous_cycle)*/
gen same_cycle = 0
replace same_cycle = 1 if (target_cycle == source_cycle)
gen previous_cycle = 0
replace previous_cycle = 1 if (target_cycle_fac == source_cycle_fac-1)

// Statistics and histogram
sum passed_difference, detail
hist passed_difference, frequency
*FIGURE 1:
graph export "$figs_dir FIG1 - number_of_days_between_passage_and_modification.png", replace

/////////////////////// All /////////////////////////
// Combine histogram
hist passed_difference, frequency by(target_cycle, col(1)) ylab(#1)
*FIGURE 2:
graph export "$figs_dir FIG2 - 4hist_full_period.png", replace

//////////////// Modified in the same cycle ///////////////////
sum passed_difference if same_cycle, detail
hist passed_difference if same_cycle, frequency 
*FIGURE 3:
graph export "$figs_dir FIG3 - amendments_hist_intraterm.png", replace

// Combined graph 
hist passed_difference if same_cycle, frequency by(target_cycle, col(1)) ylab(#1)
*FIGURE 4:
graph export "$figs_dir FIG4.png", replace

//////////////// Comparing intraterm or overlaying-term modifications ///////////////////

*Saving states of database for easier modifications.
snapshot erase _all
*Saving current, original state.
snapshot save
*Naming original database as "all_passed"
snapshot label 1 "all_passed"
/*Reducing database to those observations where the timespan between law 
adoption and modification is less than 250 days.*/
keep if passed_difference <= 250
*Saving new state.
snapshot save
*Labeling new state.
snapshot label 2 "250_passed"


snapshot restore 1 // all observations
*FIGURE 6a:
graph twoway (hist passed_difference if same_cycle | previous_cycle, frequency fcolor(black) ylabel(,angle(0)) start(0) width(31.25)) ///
(hist passed_difference if previous_cycle, frequency fcolor(gs10) ylabel(,angle(0)) start(0) width(31.25)), /// 
legend(label(1 "Intra-term")) legend(label(2 "Over-term")) ///
title("All observations") ///
name(Figure_6a, replace) nodraw
*FIGURE 6b:
snapshot restore 2 // within 250 days
graph twoway (hist passed_difference if same_cycle | previous_cycle, frequency fcolor(black) ylabel(,angle(0)) start(0) width(31.25)) ///
(hist passed_difference if previous_cycle, frequency fcolor(gs10) ylabel(,angle(0)) start(0) width(31.25)), /// 
legend(label(1 "Intra-term")) legend(label(2 "Over-term")) ///
title("Modified within 250 days") ///
name(Figure_6b, replace) nodraw


*FIGURE 6:
graph combine Figure_6a Figure_6b, title("Comparing intra-term and overlaying-term modifications") iscale(0.5) rows(2)
graph export "$figs_dir FIG6_comparing.png", width(2400) height(1600) replace

*Restoring original database if needed.
snapshot restore 1
*Erasing states from memory.
snapshot erase _all
*****************************************************************************************************


///////// LSI calculations and regressions /////////

// Clear memory and output
clear
cls

// Read first file
import excel "..\DATA\cap_laws_torvcikk_0630.xlsx", sheet("CAP - LAWS") firstrow

// destring
destring, replace // destring converts variables in varlist from string to numeric. If varlist is not specified, destring will attempt to convert all variables in the dataset from string to numeric

// Calculate LSI
gen LSI = modifications_sum
label variable LSI "Legislative Stability Index"

// Filter cases
drop if id_new == "" // Remove Fundamental Law modifications
drop if LSI == 0 
drop if LSI == .

// Take a look 
sum LSI, detail
hist LSI, frequency
graph export "$figs_dir FIG5 - LSI_histogram.png", replace

// electoral cycle string to factor
encode electoral_cycle, gen(electoral_cycle_factor)

// LSI by electoral cycle: Table 3
tabstat LSI, by(electoral_cycle_factor) statistics(count mean sd min max) save // Get all the stats by one line of code. 
*Columns cannot be named in stata13
return list
putexcel set "$other_tab_dir TABLE4", replace
putexcel A1 =  ("electoral cycle") A2 = ("N") A3= ("mean") A4 = ("sd") A5 = ("min") A6 = ("max") ///
B1 = (r(name1)) B2 = matrix(r(Stat1)) ///
C1 = (r(name2)) C2 = matrix(r(Stat2)) ///
D1 = (r(name3)) D2 = matrix(r(Stat3)) ///
E1 = (r(name4)) E2 = matrix(r(Stat4)) ///
F1 = (r(name5)) F2 = matrix(r(Stat5)) ///
G1 = (r(name6)) G2 = matrix(r(Stat6))
*The summary statistics below are added manually.
tabstat LSI, statistics(count mean sd min max) save 

////////////////////////////////////////////// First model /////////////////////////////////////////////////////////
global basic_variables i.electoral_cycle_factor pass_length_day law_length_pages  i.majortopic  i.international //Stata handles factor (categorical) variables elegantly. 
// You can prefix a variable with i. to specify indicators for each level (category) of the variable
glm LSI $basic_variables, family(gamma) link(log) vce(robust)
////////////////////////////////////////////// Basic model /////////////////////////////////////////////////////////

// Handle missing vals
replace law_type_majority = . if law_type_majority == 9999
replace vote_yes_majority = . if vote_yes_majority == 9999
replace vote_total_majority = . if vote_total_majority == 9999
replace law_type_supermajority = . if law_type_supermajority == 9999
replace vote_yes_supermajority = . if vote_yes_supermajority == 9999
replace vote_total_supermajority = . if vote_total_supermajority == 9999

// Law type supermajority
gen law_type_superm = 0 
replace law_type_superm = 1 if law_type_supermajority == 1

// Recode cabinet
gen cabinet_recoded = cabinet
replace cabinet_recoded = 1 if cabinet == 2 // Antall gov
replace cabinet_recoded = 2 if cabinet == 3 | cabinet == 4 | cabinet == 5 // Boross gov
replace cabinet_recoded = 3 if cabinet == 6 | cabinet == 7 // Horn gov
replace cabinet_recoded = 4 if cabinet == 8 | cabinet == 9 // Orban I gov
replace cabinet_recoded = 5 if cabinet == 10 | cabinet == 11 // Medgyessy gov
replace cabinet_recoded = 6 if cabinet == 12 | cabinet == 13 // Gyurcsany I gov
replace cabinet_recoded = 7 if cabinet == 14 | cabinet == 15 // Gyurcsany II gov
replace cabinet_recoded = 8 if cabinet == 16 | cabinet == 17 // Bajnai gov
replace cabinet_recoded = 9 if cabinet == 18 // Orban II gov

// Right/left
gen right = 0
replace right = 1 if cabinet_recoded == 1 | cabinet_recoded == 4 | cabinet_recoded == 9

////////////////////////////////////////////// Second model /////////////////////////////////////////////////////////
glm LSI $basic_variables right, family(gamma) link(log) vce(robust)
glm LSI $basic_variables right law_type_superm, family(gamma) link(log) vce(robust) // [This is the second model]
////////////////////////////////////////////// Second model /////////////////////////////////////////////////////////


// Rep introducer
gen rep_introducer = 0
replace rep_introducer = 1 if introducer_type == 5


////////////////////////////////////////////// Third model ////////////////////////////////////////////////////////
glm LSI $basic_variables rep_introducer, family(gamma) link(log) vce(robust)
glm LSI $basic_variables right rep_introducer, family(gamma) link(log) vce(robust)
glm LSI $basic_variables right law_type_superm rep_introducer, family(gamma) link(log) vce(robust)
////////////////////////////////////////////// Third model ////////////////////////////////////////////////////////


// Ratio of government reps
gen gov_ratio = government_reps / total_reps

// Total votes
gen total_votes = max(vote_total_majority,vote_total_supermajority)

// Absence ratio
gen absence_ratio = total_votes / total_reps

// Generate vote ratio
gen yes_ratio_majority = (vote_yes_majority / vote_total_majority) - gov_ratio
gen yes_ratio_supermajority = (vote_yes_supermajority / vote_total_supermajority) - gov_ratio
egen yes_vote_ratio = rmean(yes_ratio_majority yes_ratio_supermajority)


////////////////////////////////////////////// Fourth model ////////////////////////////////////////////////////////
glm LSI $basic_variables yes_vote_ratio, family(gamma) link(log) vce(robust)
glm LSI $basic_variables yes_vote_ratio rep_introducer, family(gamma) link(log) vce(robust)
glm LSI $basic_variables yes_vote_ratio rep_introducer right, family(gamma) link(log) vce(robust)
glm LSI $basic_variables yes_vote_ratio rep_introducer right law_type_superm, family(gamma) link(log) vce(robust)
////////////////////////////////////////////// Fourth model ////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////

/////////////////////////// Models for LSI /////////////////////////////

////////////////////////////////////////////////////////////////////////

// Model 1. 
glm LSI $basic_variables, family(gamma) link(log) vce(robust)
eststo
// Model 2. 
glm LSI $basic_variables right law_type_superm, family(gamma) link(log) vce(robust)
eststo
// Model 3. 
glm LSI $basic_variables right law_type_superm rep_introducer, family(gamma) link(log) vce(robust)
eststo
// Model 4.
glm LSI $basic_variables rep_introducer right law_type_superm yes_vote_ratio, family(gamma) link(log) vce(robust)
eststo

esttab using "$reg_est_dir TABLE2 - LSI_estimates.csv", star(* 0.10 ** 0.05 *** 0.01) label plain mtitles("(1)" "(2)" "(3)" "(4)") replace


////////////////////////////////////////////////////////////////////////

//////////////// Models for LSI - robustness analysis ///////////////////

////////////////////////////////////////////////////////////////////////

eststo clear

*recoding electoral_cycle_factor so that it contains two cycles
gen combined_electoral_cycle_factor = electoral_cycle_factor
replace combined_electoral_cycle_factor = 1 if electoral_cycle_factor == 2
replace combined_electoral_cycle_factor = 2 if electoral_cycle_factor == 3
replace combined_electoral_cycle_factor = 3 if electoral_cycle_factor == 4
replace combined_electoral_cycle_factor = 4 if electoral_cycle_factor == 5
replace combined_electoral_cycle_factor = 5 if electoral_cycle_factor == 6

label define combined_electoral_cycle_factor 1 "1990-1998" 2 "1994-2002" 3 "1998-2006" 4 "2002-2010" 5 "2006-2014", replace
label values combined_electoral_cycle_factor combined_electoral_cycle_factor

global basic_variables_robustnesscheck i.combined_electoral_cycle_factor pass_length_day law_length_pages  i.majortopic  i.international

// Model 1. 
glm LSI $basic_variables, family(gamma) link(log) vce(robust)
eststo
glm LSI $basic_variables_robustnesscheck, family(gamma) link(log) vce(robust)
eststo
// Model 2. 
glm LSI $basic_variables right law_type_superm, family(gamma) link(log) vce(robust)
eststo
glm LSI $basic_variables_robustnesscheck right law_type_superm, family(gamma) link(log) vce(robust)
eststo
// Model 3. 
glm LSI $basic_variables right law_type_superm rep_introducer, family(gamma) link(log) vce(robust)
eststo
glm LSI $basic_variables_robustnesscheck right law_type_superm rep_introducer, family(gamma) link(log) vce(robust)
eststo
// Model 4.
glm LSI $basic_variables rep_introducer right law_type_superm yes_vote_ratio, family(gamma) link(log) vce(robust)
eststo
glm LSI $basic_variables_robustnesscheck rep_introducer right law_type_superm yes_vote_ratio, family(gamma) link(log) vce(robust)
eststo

esttab using "$reg_est_dir TABLE A1 - APPENDIX - LSI_estimates_robustnesscheck.csv", star(* 0.10 ** 0.05 *** 0.01) mtitle("(1)" "(2)" "(3)" "(4)" "(5)" "(6)" "(7)" "(8)") plain label replace

